package cn.onein.edu.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.onein.edu.biz.bean.LearnContent;
import cn.onein.edu.web.common.base.BaseController;
import cn.onein.edu.web.service.LearnContentService;
import cn.onein.edu.web.service.LearnService;
import cn.onein.edu.web.util.PerPageUtil;
import cn.onein.edu.web.vo.PerpageVO;

/**
 * 育儿百科查询、Tag和文章 控制器
 * @author Upton
 *
 */
@Controller
public class LearnContentController extends BaseController{
	
	private final static Integer PERPAGESIZE = 8;
	
	Logger logger = Logger.getLogger(LearnContentController.class);
	
	@Autowired
	private LearnContentService learnContentService;
	
	@Autowired
	private LearnService learnService;
	
	private void setHotTags(HttpServletRequest req){
		req.setAttribute(REQUEST_KEY_HOT_TAGS, learnContentService.getHotTags());
	}
	
	/**
	 * 育儿百科文章显示页面
	 * @param id 文章ID
	 * @return
	 */
	@RequestMapping(value = "/learn/content/{id}.jhtml", method = {RequestMethod.GET, RequestMethod.POST})
	public ModelAndView content(@PathVariable String id,HttpServletRequest req){
		LearnContent content = null;
		Map<String,Object> model = new HashMap<String,Object>();
		setMenu(model, MENU_VALUE_LEARN);
		
		try{
			Long contentId = Long.valueOf(id);			
			content = learnContentService.learnContentById(contentId);
			model.put("content",content);
		}
		catch(Exception ex){
			logger.error("查询文章内容报错", ex);
		}
		
		try{
			learnService.getLearnMenu(model);
		}catch(Exception e){
			logger.error("Get learn menu error.", e);
			return new ModelAndView("error");
		}
		
		
		if(content == null){
			return new ModelAndView("resourceNotFound",model);
		}
		else{
			setHotTags(req);
			return new ModelAndView("learn/content",model);
		}		
	}
	
	/**
	 * 育儿百科文章查询结果页面
	 * @param searchword 查询条件
	 * @return
	 */
	@RequestMapping(value = "/learn/search.jhtml", method = {RequestMethod.GET, RequestMethod.POST})
	public ModelAndView search(String searchword,Integer currPage,HttpServletRequest req){
		Map<String,Object> model = new HashMap<String,Object>();
		Integer count = null;
		String path = req.getRequestURI();
		path = path.replace("search.jhtml", "tag");
		
		if(null == currPage){
			currPage = 1;
		}
		
		//得到记录条数
	    count = learnContentService.getContentCountBySearch(searchword);

		//得到分页BEAN
		PerpageVO perpageVO = PerPageUtil.getPerPageVO(count, currPage, PERPAGESIZE);
		
		learnContentService.getSearchContent(model, searchword, currPage, PERPAGESIZE,path);
		
		try{
			learnService.getLearnMenu(model);
		}catch(Exception e){
			logger.error("Get learn menu error.", e);
			return new ModelAndView("error");
		}
		
		
		model.put("perpageVO", perpageVO);
		setMenu(model, MENU_VALUE_LEARN);
		model.put("searchword", searchword);
				
		setHotTags(req);
		return new ModelAndView("learn/search", model);
	}
	
	/**
	 * 显示关键字相关的文章
	 * @param tag 关键字
	 * @return
	 */
	@RequestMapping(value = "/learn/tag/{tagword}", method = {RequestMethod.GET, RequestMethod.POST})
	public ModelAndView tag(@PathVariable String tagword,Integer currPage,HttpServletRequest req){
		Map<String,Object> model = new HashMap<String,Object>();
		Integer count = null;

		if(null == currPage){
			currPage = 1;
		}
		
		try{
			learnService.getLearnMenu(model);
		}catch(Exception e){
			logger.error("Get learn menu error.", e);
			return new ModelAndView("error");
		}
		
		//得到记录条数
	    count = learnContentService.getContentCountByTag(tagword);
	    
	  //得到分页BEAN
		PerpageVO perpageVO = PerPageUtil.getPerPageVO(count, currPage, PERPAGESIZE);
		
		List<LearnContent> contentList = learnContentService.getContentListBytag(tagword,currPage,PERPAGESIZE);
		
		model.put("perpageVO", perpageVO);
		model.put("contentList", contentList);
		setMenu(model, MENU_VALUE_LEARN);
		model.put("tagword", tagword);
		
		setHotTags(req);
		return new ModelAndView("learn/tag", model);
	}
}
